
package biblio.service.hire;


import biblio.dao.HireDAO;
import biblio.domain.Hire;
import biblio.util.SourceName;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Provides data to editing and deleting.
 * 
 * @author mephisto
 */
@ManagedBean(name = "hireList")
@ViewScoped
public class HireList implements Serializable
{
    public void remove()
    {
        Hire tmp = allItems.get(currentHireIndex);
        hireService.delete(tmp);
        allItems.remove(tmp);
    }

    public void store()
    {
        hireService.update(editedHire);
        allItems.set(currentHireIndex, editedHire);
    }

    public List<Hire> getAllItems()
    {
        synchronized (this)
        {
            if (allItems == null)
            {
                HireDAO hireDAO = (HireDAO)appContext.getBean("hireDAO");
                allItems = hireDAO.selectAll();
            }
        }
        return allItems;
    }

    public int getCurrentHireIndex()
    {
        return currentHireIndex;
    }

    public void setCurrentHireIndex(int currentHireIndex)
    {
        this.currentHireIndex = currentHireIndex;
    }

    public Hire getEditedHire()
    {
        return editedHire;
    }

    public void setEditedHire(Hire editedHire)
    {
        this.editedHire = editedHire;
    }

    public int getPage()
    {
        return page;
    }

    public void setPage(int page)
    {
        this.page = page;
    }

    public void setHireService(HireServiceImpl hireService)
    {
        this.hireService = hireService;
    }

    static
    {
        appContext = new ClassPathXmlApplicationContext( new String[] {
                SourceName.DAO_APPLICATION_CONTEXT
        });
    }


    private static final long serialVersionUID = -3832255132261771583L;
    private static ApplicationContext appContext;
    private static Logger log = Logger.getLogger(HireList.class);
    private int page = 1;
    private List<Hire> allItems = null;
    private int currentHireIndex;
    private Hire editedHire;
    @ManagedProperty(value="#{hireService}")
    private HireServiceImpl hireService;
}
